

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;


public class Connector implements Runnable
{
	private String name;
	private String address;
	private int port;
	
	private Thread connectorThread;
	private Socket socket;
	
	private ArrayList<ConnectorListener> listeners;
	
	private byte[] image;
	private String description;
	
	private int fileSize;
	private PrintWriter socketOut;
	private Scanner socketIn;
	
	
	public Connector(String name, String address, int port, byte[] image, int fileSize, String description)
	{
		this.name = name;
		this.address = address;
		this.port = port;
		this.image = image;
		this.fileSize = fileSize;
		this.description = description;
		
		
		connectorThread = new Thread(this);
		
		
		listeners = new ArrayList<ConnectorListener>();
	}
	
	
	public void addConnectorListener(ConnectorListener listener)
	{
		listeners.add(listener);
	}
	
	
	public void removeConnectorListener(ConnectorListener listener)
	{
		listeners.remove(listener);
	}
	
	
	public void run()
	{
		String serverAddress = null;
	 		
		try
		{
			// the following is a protocol between client and server
			System.out.println("Description is " + description);
			socket = new Socket(address, port);
			Scanner socketIn = new Scanner(socket.getInputStream());
			socketOut = new PrintWriter(socket.getOutputStream(), true);
			
			serverAddress = socket.getInetAddress().toString();
			
			fireConnectorSucceeded(serverAddress, "blah");
			
			
			
			
			socketOut.println("I45PFP_HELLO");
					
			String line = socketIn.nextLine();
			
			if (!line.startsWith("GOAHEAD"))
			{
				throw new ProtocolException("GOAHEAD expected");
			}
			
						
			socketOut.println("IMAGE " + fileSize + " " + description);
			
			line = socketIn.next();
			
			if (line.startsWith("ACCEPT"))
			{				

				int offset = 0;
				
				while (offset < fileSize)
				{
					int chunkSize = 1024;
					
					if (fileSize - offset < chunkSize)
					{
						chunkSize = fileSize - offset;
					}
					
					socket.getOutputStream().write(image, offset, chunkSize);
					offset += chunkSize;
					System.out.println(offset + " bytes written.");
				}
				
				socket.getOutputStream().flush();
				firePictureSent();
			}
			
			if (line.startsWith("REJECT"))
			{
				fireRejected();
			}
			
			
			line = socketIn.next();
			
			if (!line.startsWith("GOODBYE"))
			{
				throw new ProtocolException("GOODBYE expected");
			}
			
			fireCompleted();
			//close();
			
			 
			
					 
						
		
		}
		catch (Exception e)
		{
			fireConnectorFailed(serverAddress, e);
		}
		finally
		{
			//close();
		}
	}
	
	
	public void start()
	{
		connectorThread.start();
	}
	
	
	public void close()
	{
		try
		{
			if (socket != null)
			{
				socket.close();
			}
		}
		catch (IOException e)
		{
		}
	}
	
	// The follow methods allow the Connector class to communicate with the GUI Frame.
	
	
	public void fireConnectorSucceeded(String serverAddress, String serverName)
	{
		for (ConnectorListener listener : listeners)
		{
			listener.connectorSucceeded(serverAddress, serverName);
		}
	}


	public void fireConnectorFailed(String serverAddress, Exception reason)
	{
		for (ConnectorListener listener : listeners)
		{
			listener.connectorFailed(serverAddress, reason);
		}
	}
	
	public void firePictureSent()
	{
		for (ConnectorListener listener : listeners)
		{
			listener.pictureSent();
		}
	}
	
	public void fireRejected()
	{
		for (ConnectorListener listener : listeners)
		{
			listener.rejected();
		}
	}
	
	public void fireCompleted()
	{
		for (ConnectorListener listener : listeners)
		{
			listener.completedClient();
		}
	}
	
	public void sendText(String text)
	{
		try
		{
			socketOut.println("chat " + text);
		}
		catch(Exception e)
		{
			
		}
	}
}
